package com.gexiaobao.pigeon.ui.fragment.mine.tools.line

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.model.bean.FlyWeatherInfoResponse
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.databinding.FragmentWeatherBinding
import com.gexiaobao.pigeon.ui.adapter.MyPagerAdapter
import com.gexiaobao.pigeon.viewmodel.MatchLineQueryViewModel
import com.google.android.material.tabs.TabLayout

/**
 * @Author      : hxw
 * @Date        : 2023/6/1 9:26
 * @Describe    : 天气
 */
class FragmentWeather : BaseFragment<MatchLineQueryViewModel, FragmentWeatherBinding>() {

    @SuppressLint("CutPasteId")
    override fun initView(savedInstanceState: Bundle?) {
        try {
            val flyWeatherResult = arguments?.getParcelable<FlyWeatherInfoResponse>("fly_weather_result")
            if (flyWeatherResult == null) {
                RxToast.showToast("获取天气列表信息错误101")
                return
            }
            val fragmentList = ArrayList<Fragment>()
            val titleList = ArrayList<String>()
            fragmentList.add(FragmentToolsMapWeather.newInstance(flyWeatherResult.weatherList))
            fragmentList.add(FragmentToolsMapWeather.newInstance(flyWeatherResult.weatherList1))
            fragmentList.add(FragmentToolsMapWeather.newInstance(flyWeatherResult.weatherList2))
            fragmentList.add(FragmentToolsMapWeather.newInstance(flyWeatherResult.weatherList3))
            val sDate = flyWeatherResult.weatherList[0].reporttime.split("-")
            val sDate1 = flyWeatherResult.weatherList1[0].reporttime.split("-")
            val sDate2 = flyWeatherResult.weatherList2[0].reporttime.split("-")
            val sDate3 = flyWeatherResult.weatherList3[0].reporttime.split("-")
            if (sDate.size != 3) return
            if (sDate1.size != 3) return
            if (sDate2.size != 3) return
            if (sDate3.size != 3) return
            titleList.add("今天\n${sDate[1]}-${sDate[2]}")
            titleList.add("明天\n${sDate1[1]}-${sDate1[2]}")
            titleList.add("后天\n${sDate2[1]}-${sDate2[2]}")
            titleList.add("大后天\n${sDate3[1]}-${sDate3[2]}")
            val myPagerAdapter = context?.let { MyPagerAdapter(childFragmentManager, it, fragmentList, titleList) }
            mDatabind.viewPager.adapter = myPagerAdapter
            mDatabind.viewPager.offscreenPageLimit = 4
            mDatabind.tabLayout.setupWithViewPager(mDatabind.viewPager)//此方法就是让tablayout和ViewPager联动


            for (i in 0 until myPagerAdapter!!.count) {
                val tab: TabLayout.Tab? = mDatabind.tabLayout.getTabAt(i) //获得每一个tab
                tab!!.setCustomView(R.layout.div_bottom_nav_tab_map) //给每一个tab设置view
                if (i == 0) {
                    // 设置第一个tab的TextView是被选择的样式
                    tab.customView!!.findViewById<TextView>(R.id.tab_text).isSelected = true //第一个tab被选中
                }
                tab.customView!!.findViewById<TextView>(R.id.tab_text).text = titleList[i] //设置tab上的文字
            }


        } catch (e: Exception) {
            RxToast.showToast("获取天气列表信息错误102")
            e.printStackTrace()
        }
    }

    companion object {

        fun newInstance(flyWeatherBean: FlyWeatherInfoResponse): FragmentWeather {
            val fragment = FragmentWeather()
            val bundle = Bundle()
            fragment.arguments = bundle
            bundle.putParcelable("fly_weather_result", flyWeatherBean)
            return fragment
        }
    }
}